package Classes.GameViews.ViewPanels// ActionScript file
{
	import Classes.GameDatas.GD_Item;
	import Classes.GameDatas.GD_Player;
	import Classes.GameUtils.GU_dealPower;
	import Classes.GameViews.ViewScenes.ViewScene;
	import Classes.Managers.M_ConfigData;
	import Classes.staticinfo.Language;
	import Classes.staticinfo.StaticInfo;
	
	import fl.events.ComponentEvent;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.system.ApplicationDomain;
	
	public class VP_Package extends ViewPanel
	{
		private static var _instance:VP_Package;
		private var _interfaceUI:Sprite;
		public function VP_Package():void
		{
			this.init();
		}
		
		private function init():void
		{
			this._interfaceUI=new (ApplicationDomain.currentDomain.getDefinition("UIPackage_v") as Class);
			this.addChild(this.interfaceUI);
			this.addListener();
			this.addObserveFuncs();
		}
		
		private function addObserveFuncs():void
		{
			this.addObserveFunc(GD_Player.instance,"items",showItems);
			this.addObserveFunc(GD_Player.instance,"money",showMyJB);
			this.addObserveFunc(GD_Player.instance,"gold",showMyOB);
			this.addObserveFunc(GD_Player.instance,"QQVip",controlMoenyIcon);
			this.addObserveFunc(GD_Player.instance,StaticInfo.COLLECT_ITEM_CHANGE,refeshCollectItem,false);
		}
		
		public function controlMoenyIcon():void
		{
			this._interfaceUI["controlMoenyIcon"](M_ConfigData.instance.isOpenQQVip);
		}
		
		private function showMyJB():void
		{
			this.interfaceUI["showMyJB"](GD_Player.instance.money);
		}
		
		private function showMyOB():void
		{
			this.interfaceUI["showMyOB"](GD_Player.instance.gold);
		}
		
		/**
		 * 输入物品类型 ，进行物品过滤 
		 * @param typeStr
		 */		
		private function showItems(typeStr:String=StaticInfo.PACKAGE_ITEM_TYPE):void
		{
			var itemsAry:Array=new Array;
			var typeArr:Array=typeStr.split(",");
			var index:int=typeArr.length;
			for each(var item:GD_Item in GD_Player.instance.items)
			{
				if(item.itemNumber)
				{
					for(var i:int=0;i<index;i++)
					{
						if(int(typeArr[i])==item.itemStyle)
						{
							itemsAry.push(item);
							break;
						}
					}
				}
			}
			this.setItemData(itemsAry);
		}
		
		/**
		 * 给页面中的物品列表赋值 
		 * @param temArr
		 * 
		 */		
		private function setItemData(temArr:Array):void
		{
			temArr.sortOn("itemType",Array.NUMERIC);
			this.interfaceUI["setItemsAry"](temArr);
		}
		
		/**
		 *  注册事件 
		 * 
		 */		
		private function addListener():void
		{
			this.addEventListener("close",closeHandler);
			this.interfaceUI["_button_common"].addEventListener(ComponentEvent.BUTTON_DOWN,showCommonItemsHandler);
			this.interfaceUI["_button_collect"].addEventListener(ComponentEvent.BUTTON_DOWN,showCollectItemsHandler);
			this.addEventListener("showExChangePanel",showExChangeHandler);
			this.addEventListener("clickWish",function(e:Event):void{
				VP_errorPanel.instance.setErrorMessage(Language.noOnWish);
				VP_errorPanel.instance.enterFrame();
			});
		}
		
		
		private function showExChangeHandler(e:Event):void
		{
			VP_exChangePanel.instance.openPanel();
		}
		
		/**
		 * 物品 --类型为：101
		 * @param e
		 */		
		private function showCommonItemsHandler(e:ComponentEvent):void
		{
			this.interfaceUI["_button_common"]["lockState"]=true;
			this.interfaceUI["_button_collect"]["lockState"]=false;
			this.showItems(StaticInfo.PACKAGE_ITEM_TYPE);
			this.contorlView(false,true,true);
		}
		
		/**
		 * 收藏品 --类型为：130
		 * @param e
		 */	
		private function showCollectItemsHandler(e:ComponentEvent):void
		{
			//整个游戏的权限由自己控制   10000214系统权限---点击背包
			if(null==GD_Player.instance.playPowerObj[10000214] || 0==GD_Player.instance.playPowerObj[10000214])
			{
				VP_errorPanel.instance.noPowerMsg();
				return;
			}
			this.interfaceUI["_button_common"]["lockState"]=false;
			this.interfaceUI["_button_collect"]["lockState"]=true;
			refeshCollectItem();
			this.contorlView(true,false,true);
			this.listUpData(null);
			GU_dealPower.instance.clickDHCollectBtn();
		}
		
		/**
		 * 刷新收藏物品 
		 */		
		private function refeshCollectItem():void
		{
			this.interfaceUI["setCollectionArr"](GD_Player.instance.collectMissionArr(21));
		}
		
		/**
		 * 控制页面的隐藏显示 
		 * @param arg1 收藏
		 * @param arg2 物品
		 * @param arg3 许愿
		 */		
		private function contorlView(arg1:Boolean,arg2:Boolean,arg3:Boolean):void
		{
			this.interfaceUI["collectItemList"].visible=arg1;
			this.interfaceUI["itemList"].visible=arg2;
			this.interfaceUI["wishItemList"].visible=arg3;
		}
		
		private function removeListener():void
		{
			this.removeEventListener("close",closeHandler);
			this.interfaceUI["_button_common"].removeEventListener(ComponentEvent.BUTTON_DOWN,showCommonItemsHandler);
			this.interfaceUI["_button_collect"].removeEventListener(ComponentEvent.BUTTON_DOWN,showCollectItemsHandler);
		}
		
		private function closeHandler(e:Event):void
		{
			this.close();
			this.removeEventListener("sourceLoadOver",listUpData);
		}
		
		override public function openAt(vs:ViewScene,bg:Boolean=false,sd:Boolean=true):void
		{
			
			this.addEventListener("sourceLoadOver",listUpData);
			listUpData(null);
//			refeshCollectItem();
//			showCollectItemsHandler(null);///背包图片大
			showCommonItemsHandler(null);
			super.openAt(vs,true,sd);
			this.toCenter();
			if(null==GD_Player.instance.missions[11000012])//如果此时为新手指导 ，则默认为收藏的面板
				showCollectItemsHandler(null);
		}
		
		public function listUpData(e:Event):void
		{
			//			trace(e.target);
			//			this._interfaceUI["collectItemList"]["reshow"]();
			refeshCollectItem();
			
		}
		/*getter & setter*/
		public static function get instance():VP_Package
		{
			if(!_instance)_instance=new VP_Package;
			return _instance;
		}
		
		public function get interfaceUI():Sprite
		{
			return _interfaceUI;
		}
		
		public function showCommonItems():void
		{
			this.interfaceUI["_button_common"]["lockState"]=true;
			this.interfaceUI["_button_collect"]["lockState"]=false;
			this.showItems(StaticInfo.PACKAGE_ITEM_TYPE);
			this.contorlView(false,true,true);
		}
	}
}